#include "StdAfx.h"
#include "newGameMenu.h"


newGameMenu::GameMode newGameMenu::show(sf::RenderWindow& window)
{

	//Load menu image from file
	sf::Texture texture;
	texture.loadFromFile("Assets/Images/newGameMenu.png");
	m_mainSprite.setTexture(texture);
	m_mainSprite.setPosition(0,0);

	//Setup clickable regions

	MenuItem p1LocalButton;
	p1LocalButton.name = "p1Local";
	p1LocalButton.rect.top= 200;
	p1LocalButton.rect.height = 130;
	p1LocalButton.rect.left = 40;
	p1LocalButton.rect.width = 395;
	p1LocalButton.action = Local;
	p1LocalButton.isClicked = false;
	p1LocalButton.texture.loadFromFile("Assets/Images/localButton.png");
	p1LocalButton.sprite.setTexture(p1LocalButton.texture);
	p1LocalButton.sprite.setPosition(40,200);

	MenuItem p2LocalButton;
	p2LocalButton.name = "p2Local";
	p2LocalButton.rect.top= 200;
	p2LocalButton.rect.height = 130;
	p2LocalButton.rect.left = 585;
	p2LocalButton.rect.width = 395;
	p2LocalButton.action = Local;
	p2LocalButton.isClicked = false;
	p2LocalButton.texture.loadFromFile("Assets/Images/localButton.png");
	p2LocalButton.sprite.setTexture(p2LocalButton.texture);
	p2LocalButton.sprite.setPosition(585,200);

	MenuItem p1AIButton;
	p1AIButton.name = "p1AI";
	p1AIButton.rect.top= 333;
	p1AIButton.rect.height = 130;
	p1AIButton.rect.left = 40;
	p1AIButton.rect.width = 395;
	p1AIButton.action = AI;
	p1AIButton.isClicked = false;
	p1AIButton.texture.loadFromFile("Assets/Images/AIButton.png");
	p1AIButton.sprite.setTexture(p1AIButton.texture);
	p1AIButton.sprite.setPosition(40,333);

	MenuItem p2AIButton;
	p2AIButton.name = "p2AI";
	p2AIButton.rect.top= 333;
	p2AIButton.rect.height = 130;
	p2AIButton.rect.left = 585;
	p2AIButton.rect.width = 395;
	p2AIButton.action = AI;
	p2AIButton.isClicked = false;
	p2AIButton.texture.loadFromFile("Assets/Images/AIButton.png");
	p2AIButton.sprite.setTexture(p2AIButton.texture);
	p2AIButton.sprite.setPosition(585,333);

	MenuItem p1RemoteButton;
	p1RemoteButton.name = "p1Remote";
	p1RemoteButton.rect.top= 466;
	p1RemoteButton.rect.height = 130;
	p1RemoteButton.rect.left = 40;
	p1RemoteButton.rect.width = 395;
	p1RemoteButton.action = Remote;
	p1RemoteButton.isClicked = false;
	p1RemoteButton.texture.loadFromFile("Assets/Images/remoteButton.png");
	p1RemoteButton.sprite.setTexture(p1RemoteButton.texture);
	p1RemoteButton.sprite.setPosition(40,466);

	MenuItem p2RemoteButton;
	p2RemoteButton.name = "p2Remote";
	p2RemoteButton.rect.top= 466;
	p2RemoteButton.rect.height = 130;
	p2RemoteButton.rect.left = 585;
	p2RemoteButton.rect.width = 395;
	p2RemoteButton.action = Remote;
	p2RemoteButton.isClicked = false;
	p2RemoteButton.texture.loadFromFile("Assets/Images/remoteButton.png");
	p2RemoteButton.sprite.setTexture(p2RemoteButton.texture);
	p2RemoteButton.sprite.setPosition(585,466);

	//Exit menu item coordinates
	MenuItem goButton;
	goButton.name = "go";
	goButton.rect.left = 355;
	goButton.rect.width = 395;
	goButton.rect.top = 599;
	goButton.rect.height = 130;
	goButton.action = Go;
	goButton.isClicked = false;
	goButton.texture.loadFromFile("Assets/Images/goButtonDisabled.png");
	goButton.sprite.setTexture(goButton.texture);
	goButton.sprite.setPosition(435,599);

	_menuItems.push_back(p1LocalButton);
	_menuItems.push_back(p2LocalButton);
	_menuItems.push_back(p1AIButton);
	_menuItems.push_back(p2AIButton);
	_menuItems.push_back(p1RemoteButton);
	_menuItems.push_back(p2RemoteButton);
	_menuItems.push_back(goButton);

	return GetMenuResponse(window);
}

newGameMenu::MenuResult newGameMenu::HandleClick(int x, int y)
{
	std::list<MenuItem>::iterator it;

	for ( it = _menuItems.begin(); it != _menuItems.end(); it++)
	{
		sf::Rect<int> menuItemRect = (*it).rect;
		if( menuItemRect.contains(x,y))
			{
				if((*it).action == Local)
				{
					if((*it).isClicked == false){
						(*it).texture.loadFromFile("Assets/Images/localButtonPressed.png");
					}
					else (*it).texture.loadFromFile("Assets/Images/localButton.png");
					(*it).sprite.setTexture((*it).texture);
				}
				else if((*it).action == AI)
				{
					if((*it).isClicked == false){
						(*it).texture.loadFromFile("Assets/Images/AIButtonPressed.png");
					}
					else (*it).texture.loadFromFile("Assets/Images/AIButton.png");
					(*it).sprite.setTexture((*it).texture);
				}
				else if((*it).action == Remote)
				{
					if((*it).isClicked == false){
						(*it).texture.loadFromFile("Assets/Images/remoteButtonPressed.png");
					}
					else (*it).texture.loadFromFile("Assets/Images/remoteButton.png");
					(*it).sprite.setTexture((*it).texture);
				}
				(*it).isClicked = !(*it).isClicked;
				if((*it).isClicked)return (*it).action;
			}
	}

	return Nothing;
}

void newGameMenu::UpdateGraphics(sf::RenderWindow & window)
{
	window.clear(sf::Color(0,0,0));
	window.draw(m_mainSprite);
	std::list<MenuItem>::iterator it;
	for ( it = _menuItems.begin(); it != _menuItems.end(); it++) window.draw((*it).sprite);
	window.display();
}

newGameMenu::GameMode  newGameMenu::GetMenuResponse(sf::RenderWindow& window)
{
	sf::Event menuEvent;
	newGameMenu::MenuResult player1 = Nothing;
	newGameMenu::MenuResult player2 = Nothing;
	newGameMenu::MenuResult action = Nothing;

	while(42 != 43)
	{
		if(player1 != Nothing && player1 != Go && player2 != Nothing && player2 != Go)
		{
			std::list<MenuItem>::iterator it;
			for ( it = _menuItems.begin(); it != _menuItems.end(); it++)
			{
				if((*it).action == Go){					
					(*it).texture.loadFromFile("Assets/Images/goButtonEnabled.png");
					(*it).sprite.setTexture((*it).texture);
				}
			}
			
		}
		else
		{
			std::list<MenuItem>::iterator it;
			for ( it = _menuItems.begin(); it != _menuItems.end(); it++){
				if((*it).action == Go){					
					(*it).texture.loadFromFile("Assets/Images/goButtonDisabled.png");
					(*it).sprite.setTexture((*it).texture);
				}
			}
		}
		UpdateGraphics(window);
		while(window.pollEvent(menuEvent))
		{
			if(menuEvent.type == sf::Event::MouseButtonPressed)
			{
				action = HandleClick(menuEvent.mouseButton.x,menuEvent.mouseButton.y);
				if(action == Go)
				{
					switch (player1)
					{
						case Local:
							switch (player2)
							{
								case Local:
									return LocalvsLocal;
									break;
								case AI:
									return LocalvsAI;
									break;
								case Remote:
									return LocalvsRemote;
									break;
							}
							break;
						case AI:
							switch (player2)
							{
								case Local:
									return LocalvsAI;
									break;
								case AI:
									return AIvsAI;
									break;
								case Remote:
									return AIvsRemote;
									break;
							}
							break;
						case Remote:
							switch (player2)
							{
								case Local:
									return LocalvsRemote;
									break;
								case AI:
									return AIvsRemote;
									break;
								case Remote:
									return RemotevsRemote;
									break;
							}
							break;
					}
				}
				else
				{
					if(menuEvent.mouseButton.x >= 40 && menuEvent.mouseButton.x <= 435) player1 = action;
					else player2 = action;
				}
			}
		}
	}
}
